export interface Lyric {
  text: string
  time: number
}

const timeRexp = /\[(\d{1,2}):(\d{2})(?:\.(\d{2,3}))?\]/

export function parseLyric(lyricStr: string): Lyric[] {
  const lines = lyricStr.split('\n')
  const lyrics: Lyric[] = []
  for (const line of lines) {
    const results = timeRexp.exec(line)
    if (!results) continue
    const time1 = Number(results[1]) * 60 * 1000
    const time2 = Number(results[2]) * 1000
    const time3 =
      results.length === 2 ? Number(results[3]) * 10 : Number(results[3])
    const time = time1 + time2 + time3
    // console.log('time:', time)
    let i = 0
    let text = line.replace(timeRexp, '')
    if (text) {
      do {
        text = text.replace(timeRexp, '')
        i++
      } while (i < 3)
      lyrics.push({ time, text })
    }
  }
  return lyrics
}
